<route>{meta: {title: '博库书城数据'}}</route>
<template>
  <Page>
    <el-form ref="form" :model="data" label-width="120px" :rules="rules">
      <el-form-item label="项目路径" prop="filePath">
        <el-button @click="selectFilePath" type="primary">选择文件</el-button>
        {{data.filePath}}
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  @click="search">确定</el-button>
      </el-form-item>
    </el-form>
    <div class="tool">
      <div class="title">结果:{{currentPath}}</div>
      <div class="export">
        <el-button type="primary" @click="exportFile">导出</el-button>
      </div>
    </div>
    <div class="result">
      <Scrollbar>
        <div class="result-list">
          {{resultList}}
        </div>
      </Scrollbar>
    </div>
  </Page>
</template>

<script>
import Page from '@/components/Page';
import { ipcRenderer } from 'electron';
import file from '@/native/file';
import Scrollbar from '@/components/Scrollbar';
import dialog from '@/util/dialog';
const xlsx = require("node-xlsx");

/**
 Author: 蒋鑫强
 Time: 2023/6/6
 Description:
 */
export default {
  name: "fileSearch",
  components: {Scrollbar, Page},
  data() {
    return {
      data: {
        filePath: ''
      },
      resultList: {},
      currentPath: '',

      TYPE_MAP: {
        '教辅': '中小学教辅',
        '文学': '经典文学',
        '社科': '人文社科',
        '亲子': '亲子育儿',
        '经管': '经管励志'
      },

      rules: {
        filePath: [{required: true,message: '请选文件',trigger: 'blur'}]
      }
    }
  },

  methods: {
    selectFilePath() {
      ipcRenderer.send('open-file-dialog');
      ipcRenderer.on('get-file-dialog', (event, path) => {
        this.data.filePath = path[0];
      });
    },

    search() {
      this.$refs.form.validate(valid => {
        if (valid) {
          let list = xlsx.parse(this.data.filePath);
          const result = {};
          let allList = [];
          let ids = [];
          list.forEach(d => {
            let listData = d.data.slice(1, d.data.length).filter(res => res.length > 0);
            listData = listData.map(res => {
              return {
                paperBookId: res[0],
                paperBookName: res[2],
                author: res[3],
                brandPrice: res[4],
                exchangeCardNum: res[5],
                cover: res[6]
              };
            });
            if (d.name === '全量') {
              allList = listData;
            } else {
              ids = [
                ...ids,
                ...listData.map(res => res.paperBookId)
              ];
              result[this.TYPE_MAP[d.name]] = listData;
            }
          });
          result['other'] = allList.filter(d => !ids.includes(d.paperBookId));
          debugger;
          this.resultList = result;
        }
      });
    },

    exportFile() {
      if (this.resultList.length <= 0) {
        dialog.alert('没有需要导出的内容');
      } else {
        file.downloadFileByText('bokuData.js', `window.bokuData = ${JSON.stringify(this.resultList)}`);
      }
    }
  }
}
</script>

<style lang="less" scoped>
 .result {
   flex: 1;
   overflow: hidden;
   border: 1px solid #f1f1f1;
 }

 .tool {
   display: flex;
   justify-content: space-between;
   margin-bottom: 10px;
 }

 .result-list {
   padding: 10px;
   font-size: 12px;

   .item {
     word-break: break-all;
   }
 }
</style>
